package ve.com.wayoyo.wuml_case;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompAbstract;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompClase;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompInterface;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.Componente;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.Diagrama;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.DiagramaClases;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.VistaDiagrama;
import ve.com.wayoyo.wuml_case.Exceptions.MultiplicityException;
import android.os.Environment;
import android.widget.Toast;

public class Exporter {

	private String[] mFileList;
	private String pck = Environment.getExternalStorageDirectory() + "//WUML//UNTITLED_PROYECT//CODIGO//";
	private String[] listPath;
	private File mPath = new File(pck);	

	public void Guardar(String nombreArchivo,Diagrama diagrama ) throws IOException{
		FileOutputStream fos = null;
		try
		{
			fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/WUML/"+nombreArchivo+".txt");
			fos.write(diagrama.generarDOM().getBytes());
			fos.flush();
			fos.close();
			fos = null;
		}
		catch (IOException e)
		{
			throw e;
		}
		finally
		{
			if (fos != null)
			{
				try
				{
					fos.close();
					fos = null;
				}
				catch (IOException e)
				{
					throw e;
				}
			}
		}
	}

	public void alert(String mensaje) {
		Toast.makeText(VistaDiagrama.getVista().getContext(), mensaje, Toast.LENGTH_SHORT).show();
	}

	public void to_code(String lenguaje,Diagrama diagrama ) throws IOException{		
		if (diagrama instanceof DiagramaClases) {			
			pck = pck + ((DiagramaClases)diagrama).getPackageName();
			this.deleteFolder(pck);
//			pck = pck + ((DiagramaClases)diagrama).getPackageName();
			this.newFolder(pck);
			String root="";
			DiagramaClases diagClases = (DiagramaClases) diagrama;
			for (Componente clase : diagClases.get_components()) {
				if (UserPref.getInstancia().getLanguaje().equals("Java"))
					root = pck + "//"+clase.getTitulo()+".java";
				else
					root = pck + "//"+clase.getTitulo()+".cs";
				FileOutputStream fos = null;
				try
				{	
					String case_class = "";
					if (clase instanceof CompClase) {		
						
						try {
//							case_class = ((CompClase) clase).get_atributos().get(0).getCode(UserPref.getInstancia().getLanguaje());
//							case_class = ((CompClase) clase).get_metodos().get(1).getCode(UserPref.getInstancia().getLanguaje(),true);
							
							case_class = ((CompClase) clase).getCode(UserPref.getInstancia().getLanguaje());
						} catch (MultiplicityException e) {							
							e.printStackTrace();
						}
					}else if (clase instanceof CompInterface) {
						try {
							case_class = ((CompInterface) clase).getCode(UserPref.getInstancia().getLanguaje());
						} catch (MultiplicityException e) {							
							e.printStackTrace();
						}
					}else if (clase instanceof CompAbstract) {
						try {
							case_class = ((CompAbstract) clase).getCode(UserPref.getInstancia().getLanguaje());
						} catch (MultiplicityException e) {							
							e.printStackTrace();
						}
					}
					fos = new FileOutputStream(root);
					fos.write(case_class.getBytes());
					fos.flush();
					fos.close();
					fos = null;
				}
				catch (IOException e)
				{
					throw e;
				}
				finally
				{
					if (fos != null)
					{
						try
						{
							fos.close();
							fos = null;
						}
						catch (IOException e)
						{
							throw e;
						}
					}
				}				
			}
		}
		alert("asdf");
	}


	private void newFolder(String pck) {		
		mPath = new File(pck);
		try {
			mPath.mkdirs();			
		}catch(Exception e){

		}
	}

	private void deleteFolder(String root_act) {
		mPath = new File(root_act);
		loadFileList();

		for (String str : mFileList) {
			String root_delete = root_act + "//"+str;
			mPath = new File(root_delete);
			if (!mPath.isDirectory()){
				mPath.delete();
			}else{		
				deleteFolder( root_delete);				
			}
		}
		mPath = new File(root_act);
		mPath.delete();
	}

	private void loadFileList() {
		try {
			mPath.mkdirs();
		}
		catch(SecurityException e) {
			//	        Log.e(TAG, "unable to write on the sd card " + e.toString());
		}
		if(mPath.exists()) {
			FilenameFilter filter = new FilenameFilter() {
				public boolean accept(File dir, String filename) {
					File sel = new File(dir, filename);
					return true;
				}
			};
			mFileList = mPath.list(filter);
		}
		else {

			mFileList= new String[0];
		}
	}
}
